package com.lzx.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.answer.entity.GiveALike;
import com.lzx.answer.entity.Problem;
import com.lzx.answer.entity.vo.User;
import com.lzx.answer.feign.ESClient;
import com.lzx.answer.feign.UserClient;
import com.lzx.answer.mapper.GiveALikeMapper;
import com.lzx.answer.mapper.ProblemMapper;
import com.lzx.answer.service.GiveALikeService;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-10-05
 */
@Service
public class GiveALikeServiceImpl extends ServiceImpl<GiveALikeMapper, GiveALike> implements GiveALikeService {
    @Resource
    private GiveALikeMapper giveALikeMapper;

    @Resource
    private ProblemMapper problemMapper;

    @Resource
    private UserClient userClient;

    @Resource
    private ESClient esClient;

    //点赞问题
    @GlobalTransactional
    @Override
    public R like(GiveALike giveALike) {
        QueryWrapper<GiveALike> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", giveALike.getUserId()).eq("problem_id", giveALike.getProblemId());
        GiveALike vo = giveALikeMapper.selectOne(wrapper);
        if (vo != null) {
            if (vo.getIsLike() == 1) {
                vo.setIsLike(0);
                vo.setCreateTime(new Date());
                //取消点赞，问题表点赞人数减一
                Problem problem = problemMapper.selectById(giveALike.getProblemId());
                problem.setLikeCount(problem.getLikeCount() - 1);
                //更新问题表
                problemMapper.updateById(problem);
                //更新点赞表
                giveALikeMapper.updateById(vo);
                //同步es
                esClient.editProblem(giveALike.getProblemId());
                return R.ok().data("isLike", 0);
            } else {
                vo.setIsLike(1);
                vo.setCreateTime(new Date());
                //问题表点赞人数加一
                Problem problem = problemMapper.selectById(giveALike.getProblemId());
                problem.setLikeCount(problem.getLikeCount() + 1);
                //更新问题表
                problemMapper.updateById(problem);
                //更新点赞表
                giveALikeMapper.updateById(vo);
                //同步es
                esClient.editProblem(giveALike.getProblemId());
                return R.ok().data("isLike", 1);
            }
        } else {
            //问题表点赞人数加一
            Problem problem = problemMapper.selectById(giveALike.getProblemId());
            problem.setLikeCount(problem.getLikeCount() + 1);
            //更新问题表
            problemMapper.updateById(problem);

            //用户积分+1
            userClient.likeProblem(giveALike.getUserId());

            giveALike.setIsLike(1);
            giveALike.setCreateTime(new Date());
            giveALikeMapper.insert(giveALike);
            //同步es
            esClient.editProblem(giveALike.getProblemId());

            //发送通知
            Inform inform = new Inform();
            inform.setUserId(problem.getUserId());
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(giveALike.getUserId()));
            inform.setSender(giveALike.getUserId());
            inform.setNickname(user.getNickname());
            inform.setAvatar(user.getAvatar());
            inform.setType(InformType.LIKEPROBLEM.getType());
            inform.setMessage(problem.getTitle());
            userClient.saveInform(inform);

            return R.ok().data("isLike", 1);
        }
    }

    //从关联表获取当前用户点赞的数据
    @Override
    public R getUserGiveALikeListById(Integer userId) {
        QueryWrapper<GiveALike> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return R.ok().data("list", giveALikeMapper.selectList(wrapper));
    }
}
